package com.mac.core.action;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.mac.common.constants.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.PropertiesEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

/**
 * BaseController
 * 
 * @author macao
 *
 */
@Controller
public class BaseAction implements WebBindingInitializer {
	
	Logger log = LoggerFactory.getLogger(BaseAction.class);

	@Override
	public void initBinder(WebDataBinder webDataBinder) {
		webDataBinder.registerCustomEditor(Date.class, new DateEditor());
		webDataBinder.registerCustomEditor(Double.class, new DoubleEditor());
		webDataBinder.registerCustomEditor(Integer.class, new IntegerEditor());
	}


	/**
	 *日期格式转换
	 */
	private class DateEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            SimpleDateFormat format = new SimpleDateFormat(Constant.PATTERN_TIMESTAMP);
            Date date = null;
            if(null == text || "".equals(text)) {
            	date = null;
            } else {
            	try {
            		date = format.parse(text);
            	} catch (ParseException e) {
            		format = new SimpleDateFormat(Constant.PATTERN_DATE);
            		try {
            			date = format.parse(text);
            		} catch (ParseException e1) {
            			log.error("日期转换错误！");
            		}
            	}
            }
            setValue(date);
        }
    }
    
	/**
	 * Double类型转换
	 *
	 */
	private class DoubleEditor extends PropertiesEditor  {    
        @Override    
        public void setAsText(String text) throws IllegalArgumentException {    
        	 if (null == text || "".equals(text)) {    
             	setValue(null);    
             } else {
             	setValue(Double.parseDouble(text));    
             }  
        }    
        
        @Override    
        public String getAsText() {    
        	Object value = getValue();
        	if(null == value || "".equals(value)) {
        		return "";
        	}
            return value.toString();     
        }    
    }  
    
	/**
	 * Integer类型转换
	 *
	 */
	private class IntegerEditor extends PropertiesEditor {    
        @Override    
        public void setAsText(String text) throws IllegalArgumentException {    
            if (null == text || "".equals(text)) {    
            	setValue(null);    
            } else {
            	setValue(Integer.parseInt(text));    
            }
        }    
        
        @Override    
        public String getAsText() {
        	Object value = getValue();
        	if(null == value || "".equals(value)) {
        		return "";
        	}
            return value.toString();    
        }    
    }  

}
